/**
 * @file  数据中转站
 * @author  hj
 */
// 引入数据请求API
import {APIS} from 'configPort';
var nativeApi = require('nativeApi');
import Router from 'vue-router';

/* 加载页面 */
import Vue from 'vue';
import listPage from './vue/list.vue';
import staffMenuPage from './vue/staffMenu.vue';
Vue.use(Router);

var router = new Router();
router.map({
  '/': {
    component: listPage
  },
  '/staffMenu': {
    component: staffMenuPage
  }
});

export default {
  state: {
    loginData: {
      username: 'admin',
      password: '111111'
    },
    menuList: [],
    show: true
  },
  login: function () {
    var self = this;
    nativeApi.initAjax({
      'type': 'get',
      'url': APIS.login,
      'param': self.state.loginData,
      callback: function (result) {
        if (result && result.code === 200) {
          localStorage.setItem('login_info_test', JSON.stringify(result.data));
          router.go('/staffMenu');
        } else {
          mui.alert('登录失败' || result.msg, '提示');
        }
      }
    });
  },
  logout: function () {
    var self = this;
    nativeApi.initAjax({
      'type': 'get',
      'url': APIS.logout,
      'param': '',
      callback: function (result) {
        if (result && result.code === 200) {
          localStorage.removeItem('login_info');
          self.state.show = true;
        }
      }
    });
  },
  getcurrentuserinfo: function () {
    nativeApi.initAjax({
      'type': 'get',
      'url': APIS.getcurrentuserinfo,
      'param': '',
      callback: function (result) {
        if (result && result.code === 200) {
          localStorage.setItem('login_info_test', JSON.stringify(result.data));
        } else {
          mui.alert('请登录');
        }
      }
    });
  },
  init: function () {
    var self = this;
    nativeApi.initAjax({
      'type': 'get',
      'url': APIS.staffmenu,
      'param': '',
      callback: function (result) {
        if (result.code === 200) {
          self.state.menuList = result.data;
          console.log(self.state.menuList);
          self.getcurrentuserinfo();
        } else if (result.code === 406) {
          mui.alert('请登录' || result.msg, '提示');
        }
      }
    });
  }
};
